/*
 * $Id: PlayNotifActivity.java 289 2008-04-11 20:56:06Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.activity;

import org.devtcg.five.music.R;
import org.devtcg.five.music.service.IPlaylistMoveListener;
import org.devtcg.five.music.service.IPlaylistService;
import org.devtcg.five.music.util.PlaylistUtil;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class PlayNotifActivity extends Activity 
{
	protected IPlaylistService mService;
	protected ImageButton mPlayerNotif;
	
	private Handler mHandler = new Handler();
	
	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		bindService();
	}
	
	@Override
	public void onContentChanged()
	{
		mPlayerNotif = (ImageButton)findViewById(R.id.player_notif);
		
		if (mPlayerNotif == null)
			throw new RuntimeException("Could not create find view with id " + R.id.player_notif);
		
		mPlayerNotif.setOnClickListener(mGotoPlaylist);
	}

	@Override
	public void onDestroy()
	{
		unbindService();
		super.onDestroy();
	}

	private final OnClickListener mGotoPlaylist = new OnClickListener()
	{
		public void onClick(View v)
		{
			startActivity(new Intent(PlayNotifActivity.this, Playlist.class));
		}
	};

	private void bindService()
	{
		PlaylistUtil.bindService(this, mConnection);
	}

	private void unbindService()
	{
		try {
			if (mService != null)
				mService.unregisterOnMoveListener(mMoveListener);
		} catch (DeadObjectException e) {}

		unbindService(mConnection);
	}

	private final ServiceConnection mConnection = new ServiceConnection()
	{
		public void onServiceConnected(ComponentName name, IBinder service)
		{
			mService = IPlaylistService.Stub.asInterface(service);

			try
			{
				if (mService.isPlaying() == true)
				{
					mHandler.post(new Runnable() {
						public void run()
						{
							mPlayerNotif.setVisibility(View.VISIBLE);
						}
					});
				}

				mService.registerOnMoveListener(mMoveListener);
			}
			catch (DeadObjectException e) {}
		}

		public void onServiceDisconnected(ComponentName name)
		{
			mService = null;
		}
	};
	
	private final IPlaylistMoveListener mMoveListener = new IPlaylistMoveListener.Stub()
	{
		private final Runnable mShow = new Runnable()
		{
			public void run()
			{
				mPlayerNotif.setVisibility(View.VISIBLE);
			}
		};
		
		private final Runnable mHide = new Runnable()
		{
			public void run()
			{
				mPlayerNotif.setVisibility(View.GONE);
			}
		};
		
		public void advance() {}
		public void jump(int pos) {}
		
		public void pause()
		{
			mHandler.post(mHide);
		}

		public void play()
		{
			mHandler.post(mShow);
		}

		public void stop()
		{
			mHandler.post(mHide);
		}

		public void seek(long pos) {}
		public void skip(long offs) {}
	};
}
